# coding:utf-8
"""
description:滑动操作
"""
from common.Log import LOG


class gesture_mainpulation:

    def swipe_left(self, driver, element):
        """
        按控件左滑,x值大到小
        :param driver:
        :param element: find后element
        """
        xy = self.centreXAndY(element)
        LOG.info("左滑,x值大到小》》》")
        driver.swipe(xy[2] - 50, xy[1], xy[4] + 50, xy[1], 200)

    def swipe_right(self, driver, element):
        """
        按控件右滑，x值小到大
        :param driver:
        :param element: find后element
        """
        xy = self.centreXAndY(element)
        LOG.info("右滑，x值小到大》》》")
        driver.swipe(xy[4] + 100, xy[1], xy[2] - 50, xy[1], 200)  # x加100，防止滑动sidebar

    def swipe_down(self, driver, element):
        """
        按控件下滑，y值小到大
        :param driver:
        :param element: find后element
        """
        xy = self.centreXAndY(element)
        LOG.info("下滑，y值小到大》》》")
        driver.swipe(xy[0], xy[5] + 50, xy[0], xy[3] - 50, 200)

    def swipe_up(self, driver, element):
        """
        按控件上滑，y值大到小
        :param driver:
        :param element: find后element
        """
        xy = self.centreXAndY(element)
        LOG.info("上滑，y值大到小》》》")
        driver.swipe(xy[0], xy[3] - 50, xy[0], xy[5] + 50, 200)

    @staticmethod
    def get_sizes(driver):
        """
        返回手机的横轴x最大值（最右侧）、纵轴y最高值（最底部）
        :param driver:
        :return:
        """
        x = driver.get_window_size()['width']
        y = driver.get_window_size()['height']
        LOG.info("返回手机横轴x最大值（最右侧）、纵轴y最高值（最底部）={0}:{1}".format(x, y))
        return x, y

    @staticmethod
    def centreXAndY(element):
        """
        返回元素横轴x、纵轴y值，x值左到右变大，y值下到上变小
        :param element: find后element
        :return: 返回列表，中间x和y，右下角结束最大x和y，元素左上角开始最小x和y
        """
        """计算出控件开始坐标loc数值，左上角"""
        loc = element.location
        sizes = element.size
        """计算出控件结束坐标，右下角"""
        endx = int(loc['x'] + sizes['width'])
        endy = int(loc['y'] + sizes['height'])
        """ 计算中间点坐标"""
        centrex = int(loc['x'] + endx) / 2
        centrey = int(loc['y'] + endy) / 2
        xylist = [centrex, centrey, endx, endy, loc['x'], loc['y']]
        print(
            "x和y值，中间={0}:{1}，右下结束最大值={2}:{3}，左上开始最小值={4}:{5}".format(centrex, centrey, endx, endy, loc['x'], loc['y']))
        return xylist
